<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm"  :inline="true" v-show="showSearch" label-width="80px" class="sim_form">
      <el-form-item label="SIM卡号" prop="id" >
        <el-input  class="sim_input"
          v-model="queryParams.id"
          placeholder="请输入SIM卡号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="查询地址" prop="url">
        <el-select v-model="queryParams.url" placeholder="请选择sim卡查询地址" clearable  class="sim_input">
          <el-option
              v-for="dict in sys_sim_url"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户名" prop="userName">
        <el-select v-model="queryParams.userName" placeholder="请选择用户名" clearable  class="sim_input">
          <el-option
            v-for="dict in sys_sim_name"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用途" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择用途" clearable  class="sim_input">
          <el-option
              v-for="dict in sim_yong_tu"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="添加日期" prop="tianJiaDate">
        <el-date-picker   class="sim_input"
          v-model="queryParams.tianJiaDate"
          type="date"
          placeholder="请选择添加日期" >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="到期日期" prop="daoQiDate">
        <el-date-picker clearable  class="sim_input"
          v-model="queryParams.daoQiDate"
          type="date"
          placeholder="请选择到期日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="IMEI号" prop="imei">
        <el-input  class="sim_input"
          v-model="queryParams.imei"
          placeholder="请输入绑定的IMEI号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="SN号" prop="sn">
        <el-input class="sim_input"
          v-model="queryParams.sn"
          placeholder="请输入绑定的SN号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:sim:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:sim:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:sim:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:sim:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="simList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="sim卡号" align="center" prop="sim" />
      <el-table-column label="sim卡查询地址" align="center" prop="url"/>
      <el-table-column label="用户名" align="center" prop="userName"/>
      <el-table-column label="用途" align="center" prop="type"/>
      <el-table-column label="添加日期" align="center" prop="tianJiaDate" width="180"/>
      <el-table-column label="到期日期" align="center" prop="daoQiDate" width="180"/>
      <el-table-column label="绑定设备号" align="center" prop="imei" />
      <el-table-column label="绑定的sn号" align="center" prop="sn" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:sim:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:sim:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改sim卡信息管理对话框 -->
    <el-dialog :title="title" v-model="open" width="400px" >
      <el-form ref="form" :model="form" :rules="rules" label-width="95px">
        <el-form-item label="SIM卡前缀" prop="simQianZhui">
          <el-select v-model="form.simQianZhui" placeholder="请选择sim卡前缀" class="sim_input">
            <el-option
                v-for="dict in sys_sim_qian_zhui"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="SIM" prop="sim">
          <el-input v-model="form.sim" placeholder="请输入SIM卡号" class="sim_input"/>
        </el-form-item>

        <el-form-item label="IMEI前缀" prop="imeiQianZhui">
          <el-select v-model="form.imeiQianZhui" placeholder="请选择IMEI前缀" class="sim_input">
            <el-option
                v-for="dict in sys_imei_qian_zhui"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="IMEI号" prop="imei">
          <el-input v-model="form.imei" placeholder="请输入绑定设备号" class="sim_input"/>
        </el-form-item>
        <el-form-item label="查询地址" prop="url">
          <el-select v-model="form.url" placeholder="请选择sim卡查询地址" class="sim_input">
            <el-option
              v-for="dict in sys_sim_url"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-select v-model="form.userName" placeholder="请选择用户名" class="sim_input">
            <el-option
              v-for="dict in sys_sim_name"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用途" prop="type">
          <el-select v-model="form.type" placeholder="请选择用途" class="sim_input">
            <el-option
              v-for="dict in sim_yong_tu"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="SN号" prop="sn">
          <el-input v-model="form.sn" placeholder="请输入绑定的sn号" class="sim_input"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSim, getSim, delSim, addSim, updateSim } from "@/api/system/sim";


export default {
  name: "Sim",

  data() {
    return {
      //dicts: ['sys_sim_name', 'sys_sim_url', 'sim_yong_tu'],
      sys_sim_name:[],
      sys_sim_url:[],
      sim_yong_tu:[],
      sys_sim_qian_zhui:[],
      sys_imei_qian_zhui:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // sim卡信息管理表格数据
      simList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        url: null,
        userName: null,
        type: null,
        tianJiaDate: null,
        daoQiDate: null,
        imei: null,
        sn: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sim: [
          { required: true, message: "sim卡号不能为空", trigger: "blur" }
        ],
        url: [
          { required: true, message: "sim卡查询地址不能为空", trigger: "change" }
        ],
        userName: [
          { required: true, message: "用户名不能为空", trigger: "change" }
        ],
        type: [
          { required: true, message: "用途不能为空", trigger: "change" }
        ],
        imei: [
          { required: true, message: "绑定设备号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  mounted() {
    this.getList();
    this.getDicts("sys_sim_name").then(response => {
      this.sys_sim_name = response.data;
    });
    this.getDicts("sys_sim_url").then(response => {
      this.sys_sim_url = response.data;
    });
    this.getDicts("sim_yong_tu").then(response => {
      this.sim_yong_tu = response.data;
    });
    this.getDicts("sys_sim_qian_zhui").then(response => {
      this.sys_sim_qian_zhui = response.data;
    });
    this.getDicts("sys_imei_qian_zhui").then(response => {
      this.sys_imei_qian_zhui = response.data;
    });
    //sys_sim_qian_zhui,sys_imei_qian_zhui
  },
  methods: {
    /** 查询sim卡信息管理列表 */
    getList() {
      this.loading = true;
      listSim(this.queryParams).then(response => {
        this.simList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        url: null,
        userName: null,
        type: null,
        tianJiaDate: null,
        daoQiDate: null,
        imei: null,
        sn: null,
        sim:null,
        imeiQianZhui:null,
        simQianZhui:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加sim卡信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSim(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改sim卡信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSim(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSim(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除sim卡信息管理编号为"' + ids + '"的数据项？').then(function() {
        return delSim(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/sim/export', {
        ...this.queryParams
      }, `sim_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.sim_form{
  display: flex;
  width: 100%;
  flex-wrap: wrap;
  justify-content:left;
}
.sim_input{
  width: 200px!important;
}
</style>
